<?php
if ( !defined( "IN_OP" ) )
{
    exit( "You can't access this file directly..." );
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval( getvar( "gametype" ) );
$gType = chkGametype( $gametype );
if ( $gametype < 1 )
{
    exit();
}
if ( $curruser['acl'] != 5 )
{
    exit();
}
$chk = new CheckMode_3d( $curruser['acl'], 1, $gametype );
$chk->checkShow3dPage();
if ( $gType == 3 )
{
    $g_betname_3or5 = $g_betname_5h[$gametype];
    $g_autorate_Arr = $g_autorate_5h[$gametype];
}
elseif($gType==4)
{
	$g_betname_3or5 = judge_type( $gametype );
	$g_autorate_Arr =$g_autorate_sb;
}
else
{
    $g_betname_3or5 = judge_type( $gametype );
    if ( $gType == 1 )
    {
        $g_autorate_Arr = $g_autorate_3d;
    }
    else
    {
        $g_autorate_Arr = $g_autorate_3s;
    }
}
unset( $chk );
$tblName = $g_gametype_3d[$gametype]['ch'];
$ui = new UI_3D( true );
$tpl = new Template2();
if ( $gType == 3 )
{
    include_once( "class_3d/AutoRateConfig_ss.php" );
    $AutoRateConfig_3d = new AutoRateConfig_ss( $g_betname_3or5, $g_autorate_Arr, $gametype, $curruser['id'] );
    include_once( "class_5h/AutoRateConfig_5h.php" );
    $AutoRateConfigChk_3d = new AutoRateConfig_5h( $g_betname_3or5, $g_autorate_Arr, $gametype );
}
elseif ( $gType == 4 )
{
    include_once( "class_3d/AutoRateConfig_ss.php" );
    $AutoRateConfig_3d = new AutoRateConfig_ss( $g_betname_3or5, $g_autorate_Arr, $gametype, $curruser['id'] );
    include_once( "class_sb/AutoRateConfig_sb.php" );
    $AutoRateConfigChk_3d = new AutoRateConfig_sb( $g_betname_3or5, $g_autorate_Arr, $gametype );
}
else
{
    include_once( "class_3d/AutoRateConfig_ss.php" );
    $AutoRateConfig_3d = new AutoRateConfig_ss( $g_betname_3or5, $g_autorate_Arr, $gametype, $curruser['id'] );
    include_once( "class_3d/AutoRateConfig_3d.php" );
    $AutoRateConfigChk_3d = new AutoRateConfig_3d( $g_betname_3or5, $g_autorate_Arr, $gametype );
}
$autorateData = $AutoRateConfig_3d->getAutoRateAll();
$autorateDataChk = $AutoRateConfigChk_3d->getAutoRateAll();

if ( ispost( ) )
{
    $fields_data = array();
	foreach ( $g_betname_3or5 as $key => $val )
    {
        $bettypeArr = ($gType == 3 || $gType == 4) ? array( $key, 0 ) : explode( "_", $key );
        if ( in_array( $bettypeArr[0], $g_autorate_Arr ) )
        {
            $money = "money".$bettypeArr[0]."_".$bettypeArr[1];
            $downrate = "downrate".$bettypeArr[0]."_".$bettypeArr[1];
            $uprate = "uprate".$bettypeArr[0]."_".$bettypeArr[1];
            $typeflag = "typeflag".$bettypeArr[0]."_".$bettypeArr[1];
            $uprateData = "";
            if ( isset( $_POST[$uprate] ) )
            {
                $uprateData = floatval( trim( $_POST[$uprate] ) );
            }
            else
            {
                $uprateData = 0;
            }
            if ( isset( $_POST[$money] ) )
            {
                $moneyTemp = intval( trim( $_POST[$money] ) );
                $downrateTemp = round( floatval( trim( $_POST[$downrate] ) ), 3 );
                if ( $autorateData[$key]['money'] != $moneyTemp || $autorateData[$key]['downrate'] != $downrateTemp )
                {
                    $fields_data[] = array( "id" => $bettypeArr[0], "subid" => $bettypeArr[1], "money" => $moneyTemp < 0 ? "0" : $moneyTemp, "downrate" => $downrateTemp < 0 ? "0" : $downrateTemp, "uprate" => $uprateData, "limitrate" => 0, "typeflag" => intval( trim( $_POST[$typeflag] ) ) );
                }
            }
        }
    }
    if ( 0 < count( $fields_data ) )
    {
        $AutoRateConfig_3d->updateAutoRate( $fields_data );
        $autorateData = $AutoRateConfig_3d->getAutoRateAll();
    }
    unset( $fields_data );
}
$theadContent = "<tr class=\"title0 bgImg_{$gametype}\" align=center><td>玩法</td><td>金额(占成额)</td><td>降赔率</td><td>异动时间</td></tr>";
$tempStr = "";
$tempI = 0;
$tempClass = 2;

if ( $gametype == 20 )
{
    $showHeadArr = array( "1" => "1", "9" => "9" );
}
elseif ( $gametype>=25 )
{
	$showHeadArr=array();	
}
elseif ( 20 < $gametype )
{
    $showHeadArr = array( "1" => "1", "10" => "10" );
}
else
{
    $showHeadArr = array( "13" => "13", "20" => "20", "24" => "24", "48" => "48" );
}
if ( $autorateData == false )
{
    foreach ( $g_betname_3or5 as $key => $valName )
    {
        if ( isset( $showHeadArr[$key] ) )
        {
            $tempStr .= $theadContent;
        }
        if ( isset( $autorateDataChk[$key] ) )
        {
            $val = $autorateDataChk[$key];
            ++$tempI;
            $tempClass = $tempClass % 2 + 1;
            $tempStr .= "<tr class=\"td2 row{$tempClass}\">";
            $tempStr .= "<td class=\"td17\" align=right>{$valName}<input type=\"hidden\" value=\"".$val['typeflag']."\" name=\"typeflag".$val['bettype_id']."_".$val['subid']."\"  /></td>";
            if ( 0 < $autorateDataChk[$key]['money'] )
            {
                $tempStr .= "<td><input type=\"text\" name=\"money".$val['bettype_id']."_".$val['subid']."\" value=\"0\" size=\"8\" /></td>";
            }
            else
            {
                $tempStr .= "<td><input type=\"text\" name=\"money".$val['bettype_id']."_".$val['subid']."\" value=\"0\" size=\"8\" disabled />(未开放)</td>";
            }
            $tempStr .= "<td><input type=\"text\" name=\"downrate".$val['bettype_id']."_".$val['subid']."\" value=\"".( $val['downrate'] < 0 ? "0" : $val['downrate'] )."\" size=\"5\" /><input type=\"hidden\" name=\"uprate".$val['bettype_id']."_".$val['subid']."\" value=\"".$val['uprate']."\" size=\"5\" /></td>";
            $tempStr .= "<td>0000-00-00 00:00:00</td>";
            $tempStr .= "</tr>";
            $dataArr = array( "user_id" => $curruser['id'], "money" => 0, "downrate" => $val['downrate'], "uprate" => 0, "limitrate" => 1, "id" => $val['bettype_id'], "subid" => $val['subid'], "typeflag" => $val['typeflag'], "gametype" => $gametype );
            $AutoRateConfig_3d->insertAutoRate( $dataArr );
        }
        else
        {
            $tempStr .= "<tr class=\"td2 row{$tempClass}\"><th>{$valName}</th><td colspan=5 align=center>查无此玩法的相关资料，请洽系统管理者</td></tr>";
        }
    }
}
else
{
    foreach ( $g_betname_3or5 as $key => $valName )
    {
        if ( isset( $showHeadArr[$key] ) )
        {
            $tempStr .= $theadContent;
        }
        if ( isset( $autorateData[$key] ) )
        {
            $val = $autorateData[$key];
            ++$tempI;
            $tempClass = $tempClass % 2 + 1;
            $tempStr .= "<tr class=\"td2 row{$tempClass}\">";
            $tempStr .= "<td class=\"td17\" align=right>{$valName}<input type=\"hidden\" value=\"".$val['typeflag']."\" name=\"typeflag".$val['bettype']."_".$val['subid']."\"  /></td>";
            if ( isset( $autorateDataChk[$key]['money'] ) && 0 < $autorateDataChk[$key]['money'] )
            {
                $tempStr .= "<td><input type=\"text\" name=\"money".$val['bettype']."_".$val['subid']."\" value=\"".( $val['money'] < 0 ? "0" : $val['money'] )."\" size=\"8\" /></td>";
            }
            else
            {
                $tempStr .= "<td><input type=\"text\" name=\"money".$val['bettype']."_".$val['subid']."\" value=\"".( $val['money'] < 0 ? "0" : $val['money'] )."\" size=\"8\" disabled />(未开放)</td>";
            }
            $tempStr .= "<td><input type=\"text\" name=\"downrate".$val['bettype']."_".$val['subid']."\" value=\"".( $val['downrate'] < 0 ? "0" : $val['downrate'] )."\" size=\"5\" /><input type=\"hidden\" name=\"uprate".$val['bettype']."_".$val['subid']."\" value=\"".$val['uprate']."\" size=\"5\" /></td>";
            $tempStr .= "<td>".$val['updatetime']."</td>";
            $tempStr .= "</tr>";
        }
        else if ( isset( $autorateDataChk[$key] ) )
        {
            $val = $autorateDataChk[$key];
            ++$tempI;
            $tempClass = $tempClass % 2 + 1;
            $tempStr .= "<tr class=\"td2 row{$tempClass}\">";
            $tempStr .= "<td class=\"td17\" align=right>{$valName}<input type=\"hidden\" value=\"".$val['typeflag']."\" name=\"typeflag".$val['bettype_id']."_".$val['subid']."\"  /></td>";
            if ( 0 < $autorateDataChk[$key]['money'] )
            {
                $tempStr .= "<td><input type=\"text\" name=\"money".$val['bettype_id']."_".$val['subid']."\" value=\"0\" size=\"8\" /></td>";
            }
            else
            {
                $tempStr .= "<td><input type=\"text\" name=\"money".$val['bettype_id']."_".$val['subid']."\" value=\"0\" size=\"8\" disabled />(未开放)</td>";
            }
            $tempStr .= "<td><input type=\"text\" name=\"downrate".$val['bettype_id']."_".$val['subid']."\" value=\"".$val['downrate']."\" size=\"5\" /><input type=\"hidden\" name=\"uprate".$val['bettype_id']."_".$val['subid']."\" value=\"".( $val['uprate'] < 0 ? "0" : $val['uprate'] )."\" size=\"5\" /></td>";
            $tempStr .= "<td>0000-00-00 00:00:00</td>";
            $tempStr .= "</tr>";
            $dataArr = array( "user_id" => $curruser['id'], "money" => 0, "downrate" => $val['downrate'], "uprate" => 0, "limitrate" => 1, "id" => $val['bettype_id'], "subid" => $val['subid'], "typeflag" => $val['typeflag'], "gametype" => $gametype );
            $AutoRateConfig_3d->insertAutoRate( $dataArr );
        }
    }
    unset( $autorateData );
}
$tpl->load( "main", "autorate_ss.htm" );
$tpl->set( array( "action" => "op.php?op=subsidary&fp=autorate_ss&gametype={$gametype}", "reload" => "op.php?op=subsidary&fp=autorate_ss&gametype={$gametype}", "tbodycontente" => $tempStr, "tblName" => $tblName, "theadContent" => $theadContent, "gametype" => $gametype ) );
$ui->set( "csslink", "<link REL=\"StyleSheet\" type=\"text/css\" href=\"core/tpl/k2/rate3d.css\">" );
$ui->header();
$tpl->show( "main" );
$ui->footer();
?>